package com.dgw.config;

import com.dgw.interceptor.LoginIntercepter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;


/**
 * @program: spring-boot-web-restfulcrud
 * @description:
 * @author: Mr.Dai
 * @create: 2019-11-29 16:53
 **/
@Configuration
public class AppConfig extends WebMvcConfigurationSupport {
    /**
     * URL 映射
     */
    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");
        registry.addViewController("/index").setViewName("login");
        registry.addViewController("/test").setViewName("test");
        // 配置 dashboard.html 映射
        registry.addViewController("/main.html").setViewName("dashboard");
        super.addViewControllers(registry);
    }

    /**
     * 资源路径 映射
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/resources")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/templates")
                .addResourceLocations("classpath:/public");
        /**
         * 支持webjars
         */
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        /**
         * 支持swagger
         */
        // 解决 SWAGGER 404报错
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        super.addResourceHandlers(registry);
    }
    /**
     * 拦截器映射
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
        /**
         * Spring mvc 需要对静态资源排除 spring boot 不需要排除
         */
     registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/**")
             .excludePathPatterns("/user/login","/","/index","/testerror")
                // swagger 排除规则
                .excludePathPatterns("/swagger-ui.html")
                .excludePathPatterns("/swagger-resources/**")
                .excludePathPatterns("/error")
                .excludePathPatterns("/webjars/**");
        super.addInterceptors(registry);
    }
    /**
     * 配置国际化
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        // 默认使用的语言
        slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 参数名 用于区别使用语言类型
        /**
         * <a class="btn btn-sm" th:href="@{index.html(l='en-US')}">中文</a>
         * <a class="btn btn-sm" th:href="@{index.html(l='zh-CN')}">English</a>
         */
        lci.setParamName("l");
        return lci;
    }

    /**
     * tomcat定制
     */
}
